using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=CANH-PC;Initial Catalog=anh;User ID=sa; Password=123456");
            SqlCommand comm = new SqlCommand("select ima from image where id=1",conn);
            SqlDataAdapter adapter = new SqlDataAdapter(comm);
            DataSet ds = new DataSet("anh");
            byte[] mydata = new byte[0];
            adapter.Fill(ds, "anh");
            DataRow myrow;
            myrow = ds.Tables["anh"].Rows[0];
            mydata = (byte[])myrow["ima"];
            MemoryStream stream = new MemoryStream(mydata);
            pictureBox1.Image = Image.FromStream(stream);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;




        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=CANH-PC;Initial Catalog=anh;User ID=sa; Password=123456");
            SqlCommand comm = new SqlCommand("insert into image (id,ima) values(1,@Pic)", conn);
            MemoryStream stream = new MemoryStream();
            pictureBox2.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[]pic =stream.ToArray();
            comm.Parameters.AddWithValue("@Pic",pic);
            conn.Open();
            comm.ExecuteNonQuery();

        }
    }
}